﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Pibby.Core.Tests.Stubs
{
	public class OpmlDocumentGenerator : IGenerateContent
	{
		private StringBuilder _builder = new StringBuilder();

		#region Constructors

		public OpmlDocumentGenerator ()
			: this (1)
		{
		}

		public OpmlDocumentGenerator(int howManyItems)
			: this(howManyItems, DateTime.Today)
		{
		}

		public OpmlDocumentGenerator(int howManyItems, DateTime pubDate)
		{
			this.HowManyItems = howManyItems;
			this.PublicationDate = pubDate;

			this.OpmlVersion = "2.0";
			this.TitleText = "Test";
			this.OwnerName = "UnitTests";
			this.OwnerId = "UnitTestsId";
			this.OutlineFormatString = "podcast{0}";
		}

		#endregion

		#region Properties

		public string OpmlVersion
		{
			get;
			set;
		}

		public string TitleText
		{
			get;
			set;
		}

		public string OwnerName
		{
			get;
			set;
		}

		public string OwnerId
		{
			get;
			set;
		}

		public string OutlineFormatString
		{
			get;
			set;
		}

		public int HowManyItems
		{
			get;
			set;
		}

		public DateTime PublicationDate
		{
			get;
			set;
		}

		#endregion

		private void CreateOpml()
		{
			BuildHeader(TitleText, OwnerName, OwnerId, this.PublicationDate);

			for (int i = 0; i < this.HowManyItems; ++i)
			{
				string item = string.Format(OutlineFormatString, i + 1);
				string webaddress = "http://" + item;

				BuildItem(item, item, webaddress, webaddress + "/" + item);
			}

			BuildFooter();
		}

		public void BuildHeader(string title, string ownerName, string ownerId, DateTime publicationDate)
		{
			this._builder.AppendFormat("<opml version=\"{0}\">", this.OpmlVersion);

			this._builder.AppendLine("<head>");
			this._builder.AppendFormat("<title>{0}</title>\n", title);
			this._builder.AppendFormat("<ownerName>{0}</ownerName>", ownerName);
			this._builder.AppendFormat("<ownerId>{0}</ownerId>\n", ownerId);
			this._builder.AppendFormat("<dateModified>{0}</dateModified>", OpmlDateFormat(publicationDate));
			this._builder.AppendLine("</head>");
			this._builder.AppendLine("<body>");
		}

		public void BuildItem(string text, string title, string feedUrl, string webpage)
		{
			this._builder.AppendFormat("<outline text=\"{0}\" title=\"{1}\" type=\"rss\" xmlUrl=\"{2}\" htmlUrl=\"{3}\"/>",
									text, title, feedUrl, webpage);
		}


		public void BuildFooter()
		{
			this._builder.AppendLine("</body>");
			this._builder.AppendLine("</opml>");
		}


		private string OpmlDateFormat(DateTime dt)
		{
			// RFC822 format
			return string.Format("{0:ddd, dd MMM yyyy HH:mm:ss} +0000", dt);
		}

		#region IGenerateContent

		public string Content
		{
			get
			{

				this.CreateOpml();

				return this._builder.ToString();
			}
		}

		public TextReader ContentStream
		{
			get
			{
				return new StringReader(this.Content);
			}
		}


		#endregion

	}
}
